package com.milo.xml;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.InputStream;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * xml示例报文
 * <p>
 *     <dingxi dn="day">
 *     <city cityX="219" cityY="170" cityname="临洮县" centername="临洮县" fontColor="FFFFFF" pyName="" state1="2" state2="2" stateDetailed="阴" tem1="14" tem2="2" temNow="15" windState="东风3-4级" windDir="东风" windPower="3级" humidity="21%" time="15:00" url="101160205"/>
 *     <city cityX="256" cityY="231" cityname="渭源县" centername="渭源县" fontColor="FFFFFF" pyName="" state1="2" state2="2" stateDetailed="阴" tem1="13" tem2="2" temNow="11" windState="小于" windDir="东风" windPower="2级" humidity="23%" time="15:00" url="101160204"/>
 *     <city cityX="324" cityY="141" cityname="定西市" centername="定西市" fontColor="FFFF00" pyName="" state1="2" state2="2" stateDetailed="阴" tem1="10" tem2="1" temNow="7" windState="小于" windDir="东风" windPower="3级" humidity="51%" time="15:00" url="101160201"/>
 *     <city cityX="325" cityY="244" cityname="陇西县" centername="陇西县" fontColor="FFFFFF" pyName="" state1="2" state2="2" stateDetailed="阴" tem1="14" tem2="3" temNow="11" windState="小于" windDir="东北风" windPower="2级" humidity="28%" time="15:00" url="101160203"/>
 *     <city cityX="417" cityY="211" cityname="通渭县" centername="通渭县" fontColor="FFFFFF" pyName="" state1="2" state2="2" stateDetailed="阴" tem1="14" tem2="2" temNow="9" windState="小于" windDir="东北风" windPower="3级" humidity="48%" time="15:00" url="101160202"/>
 *     <city cityX="287" cityY="307" cityname="漳县" centername="漳县" fontColor="FFFFFF" pyName="" state1="2" state2="2" stateDetailed="阴" tem1="13" tem2="1" temNow="11" windState="小于" windDir="东风" windPower="3级" humidity="20%" time="15:00" url="101160206"/>
 *     <city cityX="216" cityY="394" cityname="岷县" centername="岷县" fontColor="FFFFFF" pyName="" state1="2" state2="2" stateDetailed="阴" tem1="12" tem2="2" temNow="11" windState="小于" windDir="东风" windPower="2级" humidity="27%" time="15:00" url="101160207"/>
 * </dingxi>
 * </p>
 * 天气接口xml信息
 * @author Milo Lee
 * @date 2022-04-11 16:12
 *
 */
@Data
@Slf4j
@XStreamAlias("dingxi")
public class SkyXmlMessage implements Serializable {
  private static final long serialVersionUID = -3586245291677274914L;

  /**
   * 使用dom4j解析的存放所有xml属性和值的map.
   */
  private Map<String, Object> allFieldsMap;

  ///////////////////////
  // 天气接口推送xml属性
  ///////////////////////

  @XStreamImplicit(itemFieldName = "city")//节点注解(必须写)
  private List<CityBean> city;



  public static SkyXmlMessage fromXml(String xml) {
    //修改微信变态的消息内容格式，方便解析
    xml = xml.replace("<?xml version=\\\"1.0\\\" encoding=\\\"gbk\\\"?>", "");
    final SkyXmlMessage xmlMessage = XStreamTransformer.fromXml(SkyXmlMessage.class, xml);
    //xmlMessage.setAllFieldsMap(XmlUtils.xml2Map(xml));
    return xmlMessage;
  }

  public static SkyXmlMessage fromXml(InputStream is) {
    return XStreamTransformer.fromXml(SkyXmlMessage.class, is);
  }

}
